package moe.oil.db.api;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import moe.oil.dto.user.UserTargetDTO;
import moe.oil.db.service.UserTargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * author: 紫月
 * Date: 2022/04/16 17:33
 * Description:
 *
 * @author - -
 */
@RestController
@Slf4j
@Setter(onMethod_ = @Autowired)
@RequestMapping("/userTarget")
@CrossOrigin
public class UserTargetController {
    private UserTargetService userTargetService;

    // 查询全部
    @GetMapping
    public List<UserTargetDTO> getUserTarget(int page, int size) {
        return userTargetService.findAll(PageRequest.of(page, size));
    }

    // 根据id查询
    @GetMapping("/{id}")
    public Optional<UserTargetDTO> getUserTargetById(@PathVariable("id") Long id) {
        return userTargetService.findById(id);
    }

    // 添加
    @PostMapping("/")
    public UserTargetDTO addUserTarget(@Validated UserTargetDTO userTargetDTO) {
        return userTargetService.add(userTargetDTO).get();
    }

    // 根据id删除
    @DeleteMapping("/{id}")
    public int deleteUserTargetById(@PathVariable("id") Long id) {
        return userTargetService.deleteById(id);
    }

    // 修改
    @PutMapping("/")
    public UserTargetDTO updateUserTarget(@Validated UserTargetDTO userTargetDTO) {
        return userTargetService.update(userTargetDTO).get();
    }
}
